/*================================================================================
*
*
*                                   
*
*
* 项目名称: mcp2515-spi-can-demo
* 文件名称: RGB_LED.c
* 创建日期: 21-01-04
* 文件描述: 
*
================================================================================*/
#include "tim.h"
#include "RGB_LED.h"

//-public data-------------------------------------------------------------------
RgbLed_t g_LedState;

//-private data------------------------------------------------------------------
RgbLed_t s_LedCtrl;

/********************************************************************************
*函数功能: 
    RGB LED 初始化
*修改记录: 
*日期:                作者:                摘要:
      21-01-04             YummoShin            Create
********************************************************************************/
void InitRgbLed(void)
{
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
    HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1);

    __HAL_TIM_ENABLE_IT(&htim5, TIM_IT_CC1|TIM_IT_UPDATE);
}

/********************************************************************************
*函数功能: 
    LED 运行处理主函数
*修改记录: 
*日期:                作者:                摘要:
      21-01-04             YummoShin            Create
********************************************************************************/
void ProcessRgbLed(void)
{
    static uint8_t s_u8R=0, s_u8G=0,s_u8B=0, s_u8L=0;
    static int16_t s_i16StpR=0, s_i16StpG, s_i16StpB, s_i16StpL; //每次调节步长
    static uint8_t i=1, s_flgSetCmp=1;
    static uint32_t s_u32Tick=0;

    if( 
        ( (g_LedState.u8R!=s_LedCtrl.u8R) ||
          (g_LedState.u8G!=s_LedCtrl.u8G) ||
          (g_LedState.u8B!=s_LedCtrl.u8B) ||
          (g_LedState.u8L!=s_LedCtrl.u8L)    ) &&
        (g_LedState.flgAdj==0)
    ){
        g_LedState.flgAdj = 1;

        s_u8R = s_LedCtrl.u8R;
        s_u8G = s_LedCtrl.u8G;
        s_u8B = s_LedCtrl.u8B;
        s_u8L = s_LedCtrl.u8L;

        s_i16StpR = (s_u8R-g_LedState.u8R); // 4/4
        s_i16StpG = (s_u8G-g_LedState.u8G); // 4/4
        s_i16StpB = (s_u8B-g_LedState.u8B); //4/4
        s_i16StpL = (s_u8L-g_LedState.u8L)*16; //64/4
    }

    if(1==g_LedState.flgAdj) //正在调节状态
    {
        if(s_flgSetCmp==0)
        {
            if((HAL_GetTick()-s_u32Tick) > 125) //250ms overtime
            {
                s_flgSetCmp = 1;
            }
        }
        
        if(s_flgSetCmp)
        {
            g_LedState.u8R = (s_u8R-s_i16StpR)+s_i16StpR*i/4;
            g_LedState.u8G = (s_u8G-s_i16StpG)+s_i16StpG*i/4;
            g_LedState.u8B = (s_u8B-s_i16StpB)+s_i16StpB*i/4;
            g_LedState.u8L = (s_u8L-s_i16StpL/16)+s_i16StpL*i/64;

            __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_2, s_u8R*4-(4-i)*s_i16StpR); //channel R
            __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_3, s_u8G*4-(4-i)*s_i16StpG); //channel G
            __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_4, s_u8B*4-(4-i)*s_i16StpB); //channel B
            __HAL_TIM_SetCompare(&htim5, TIM_CHANNEL_1, s_u8L*64-(4-i)*s_i16StpL); //Luminance

            i++;
            s_u32Tick = HAL_GetTick();
            s_flgSetCmp = 0;
        }

        if(i==5)
        {
            i = 1;
            s_flgSetCmp = 1;
            g_LedState.flgAdj = 0;
        }
    }
}

/********************************************************************************
*函数功能: 
    装载TIM CC寄存器更改LED属性
    @param pLedInfo, LED 的各属性
*修改记录: 
*日期:                作者:                摘要:
      21-02-05             YummoShin            Create
********************************************************************************/
void RgbLedCtrl(const RgbLed_t *pLedInfo)
{
    s_LedCtrl = *pLedInfo;
}

/********************************************************************************
*函数功能: 
    计数匹配中断回调函数
*修改记录: 
*日期:                作者:                摘要:
      21-01-05             YummoShin            Create
********************************************************************************/
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim==&htim5)
    {
        htim3.Instance->CCMR1 = 0x4000; //0100 0000 0000 0000 Force deactive level
        htim3.Instance->CCMR2 = 0x4040; //0100 0000 0100 0000
    }
}

/********************************************************************************
*函数功能: 
    计数更新中断回调函数
*修改记录: 
*日期:                作者:                摘要:
      21-01-05             YummoShin            Create
********************************************************************************/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim==&htim5)
    {
        if(g_LedState.u8L!=0)
        {
            htim3.Instance->CCMR1 = 0x6000; //0110 0000 0000 0000 PWM mode 1
            htim3.Instance->CCMR2 = 0x6060; //0110 0000 0110 0000
        }
    }
}
